package com.dongdongshop.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.dongdongshop.data.Result;
import com.dongdongshop.em.ConstantEnum;
import com.dongdongshop.pojo.*;
import com.dongdongshop.service.AddressService;
import org.apache.shiro.SecurityUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("address")
public class TbAddressController {

    @Reference
    private AddressService addressService;


    @RequestMapping("getAddressInfo")
    @ResponseBody
    public Result getAreasInfo(){
        TbUser user = (TbUser) SecurityUtils.getSubject().getPrincipal();
        if (user == null){
            return Result.build(ConstantEnum.NOT_LOG);
        }
        List<TbAddress> list = addressService.getAddressInfo(user.getUsername());
        return Result.build(ConstantEnum.SUCCESS).setResult(list);
    }

    @RequestMapping("insertAddressInfo")
    @ResponseBody
    public Result insertAddressInfo(TbAddress tbAddress){
        TbUser user = (TbUser) SecurityUtils.getSubject().getPrincipal();
        tbAddress.setUserId(user.getUsername());
        addressService.insertAddressInfo(tbAddress);
        return Result.build(ConstantEnum.SUCCESS);
    }

    @RequestMapping("getProvinceInfo")
    @ResponseBody
    public Result getProvinceInfo(){
        List<TbProvinces> list = addressService.getProvinceInfo();
        return Result.build(ConstantEnum.SUCCESS).setResult(list);

    }

    @RequestMapping("getCityInfo")
    @ResponseBody
    public Result getCityInfo(String id){
        List<TbCities> list = addressService.getCityInfo(id);
        return Result.build(ConstantEnum.SUCCESS).setResult(list);
    }

    @RequestMapping("getAreaInfo")
    @ResponseBody
    public Result getAreaInfo(String id){
        List<TbAreas> list = addressService.getAreaInfo(id);
        return Result.build(ConstantEnum.SUCCESS).setResult(list);
    }
}
